package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IBankFinder;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.GetCashrule;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.UserBalance;
import com.gears_software.sxdc.dccx.data.model.UserBankCardInformation;
import com.gears_software.sxdc.dccx.data.model.request.RCashModel;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by SunMuran on 2017/7/28.
 * 余额提现
 */
@EActivity(R.layout.yu_e_tixian)
public class YuETiXianActivity extends Activity {

    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    Button btn_back;
    @ViewById
    TextView tv_money;
    @ViewById
    EditText et_money;
    @ViewById
    TextView tv_all;
    @ViewById
    TextView tv_time;
    @ViewById
    Button btn_withdraw_deposit;
    @RestService
    IBankFinder bankFinder;
    @Bean
    GearsErrorHandler errorHandler;
    MaterialDialogProgress progress;
    GetCashrule getCashrule;
    String week = "";
    int hour = 0;
    int starttime = 0;
    int endtime = 0;

    @AfterViews
    void created() {
        bankFinder.setRestErrorHandler(errorHandler);
        actionbar_title.setText("余额提现");
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        initIconfont();
        progress = new MaterialDialogProgress(this);
        progress.show("正在加载...");
        getUserBalance();
        getCashrule();


    }

    void initIconfont() {
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        right_icon.setVisibility(View.GONE);
    }

    @Click
    void tv_all() {
        et_money.setText(tv_money.getText());
    }

    @Click
    void back_icon() {
        finish();
    }

    @Click
    void btn_back() {
        finish();
    }

    @Background
    void getUserBalance() {
        ResultModel<UserBalance> res = bankFinder.getUserBalance();
        showResout(res);
    }

    @UiThread
    void showResout(ResultModel<UserBalance> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    UserBalance balance = res.getData();
                    tv_money.setText(balance.getCarpoolamount() + "");
                }
            }
        }
        progress.dismiss();
    }

    @Background
    void getCashrule() {
        String type = "2";
        ResultModel<GetCashrule> resultModel = bankFinder.getCashrule(type);
        showCashruleResult(resultModel);
    }

    @UiThread
    void showCashruleResult(ResultModel<GetCashrule> resultModel) {
        if (resultModel != null) {
            if (resultModel.getErrno().equals("0")) {
                if (resultModel.getData() != null) {
                    getCashrule = resultModel.getData();
                    tv_time.setText("2.每天可提现一次,支持提现时间段:"
                            + "周" + getCashrule.getMon() + "," + "上午" + getCashrule.getStarttime().substring(0, 2)
                            + ":" + getCashrule.getStarttime().substring(2, 4) + "-" + getCashrule.getEndtime()
                            .substring(0, 2) + ":" + getCashrule.getEndtime().substring(2, 4) + "。");
                    checkTX();
                }
            }
        }
        progress.dismiss();
    }

    private void checkTX(){
        Calendar calendar = Calendar.getInstance();
        week = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK) - 1);
        SimpleDateFormat sf = new SimpleDateFormat("HHmm");
        hour = Integer.parseInt(sf.format(calendar.getTime()));
        starttime = Integer.parseInt(getCashrule.getStarttime());
        endtime = Integer.parseInt(getCashrule.getEndtime());
        if (week.equals(getCashrule.getMon()) && (hour >=starttime && hour<= endtime)){
            btn_withdraw_deposit.setBackgroundColor(ContextCompat.getColor(this,R.color.colorActionbar));
        }
    }

    @Click
    void btn_withdraw_deposit() {

        if (!week.equals(getCashrule.getMon())) {
            TastyToast.makeText(this, "每周周三才可以提现", app.SHORT, app.CONFUSING);
        } else if (hour < starttime || hour > endtime) {
            TastyToast.makeText(this, "请在09:00至18:00时间断为提现 ", app.SHORT, app.CONFUSING);
        } else {
            if (et_money.getText().toString().equals("")) {
                TastyToast.makeText(this, "请输入提现金额", app.SHORT, app.WARNING);
            } else if (new BigDecimal(et_money.getText().toString()).compareTo(new BigDecimal(0)) == -1) {
                TastyToast.makeText(this, "提现金额不得小于0", app.SHORT, app.WARNING);
            } else if (new BigDecimal(et_money.getText().toString()).compareTo(new BigDecimal(tv_money.getText().toString())) == 1) {
                TastyToast.makeText(this, "提现金额不得大于可提现金额", app.SHORT, app.WARNING);
            } else {
                progress.show("正在提现...");
                getBankInfo();
            }
        }

    }

    @Background
    void getBankInfo() {
        ResultModel<UserBankCardInformation> res = bankFinder.getUserBankCardInformation();
        showBankInfoResult(res);
    }

    @UiThread
    void showBankInfoResult(ResultModel<UserBankCardInformation> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    UserBankCardInformation bankCardInformation = res.getData();
                    goCash();
                } else {
                    TastyToast.makeText(this, "您还没有绑定银行卡，不能提现。", app.SHORT, app.WARNING);
                }
            }
        }
        progress.dismiss();
    }

    @Background
    void goCash() {
        RCashModel cashModel = new RCashModel();
        cashModel.setType("2");
        cashModel.setAmount(et_money.getText().toString().trim());
        ResultModel<String> res = bankFinder.getCash(cashModel);
        showCashResult(res);
    }

    @UiThread
    void showCashResult(ResultModel<String> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                TastyToast.makeText(this, "提现申请成功，两个工作日内到账。", app.SHORT, app.SUCCESS);
                this.finish();
            } else {
                TastyToast.makeText(this, res.getErrmsg(), app.SHORT, app.ERROR);
            }
        }
        progress.dismiss();
    }
}
